var ReleaseLimitEdit = $("#ReleaseLimitEdit").val();
var d = new Date();
var month = (d.getMonth()+1) < 10 ? "0"+(d.getMonth()+1) : (d.getMonth()+1);
var day = d.getDate() < 10 ? "0"+d.getDate() : d.getDate();
var curDate = d.getFullYear()+"-"+month+"-"+day;
var dtGridColumns = [{
    id : 'releaseDate',
    title : '日期',
    type : 'string',
    columnClass : 'text-center',
    headerStyle : 'color:grey;'
},{
    id : 'amountLimit',
    title : '当日放款额度上限（元）',
    type : 'number',
    columnClass : 'text-center',
    headerStyle : 'color:grey;'
},{
    id : 'releasedAmount',
    title : '已放款额度（元）',
    type : 'number',
    columnClass : 'text-center',
    headerStyle : 'color:grey;'
},{
    id : 'preReleaseAmount',
    title : '待放款额度（元）',
    type : 'number',
    columnClass : 'text-center',
    headerStyle : 'color:grey;'
},{
    id : 'operation',
    title : '操作',
    type : 'string',
    columnClass : 'text-center tpl-table-black-operation',
    headerStyle : 'color:grey;',
    resolution:function(value, record, column, grid, dataNo, columnNo){
        var content = '';
        if(ReleaseLimitEdit && curDate == record.releaseDate){
           content += '<a href="#" onclick="editReleaseLimit(\''+record.releaseDate+'\','+record.limitId+','+record.amountLimit+');" class="" >编辑</a>';
        }
        return content;
    }
}];

var dtGridOption = {
    lang : 'zh-cn',
    ajaxLoad : true,
    check : true,
    checkWidth :'37px',
    loadURL : 'releaseLimit/releaseLimitList',
    columns : dtGridColumns,
    gridContainer : 'dtGridContainer',
    toolbarContainer : 'dtGridToolBarContainer',
    tools : '',
    pageSize : 10,
    pageSizeLimit : [10, 20, 30]
};
var grid = $.fn.dlshouwen.grid.init(dtGridOption);


$(function() {
	grid.load();
	$('#status').selected();
	$('.form-datetime-lang').datepicker({
	    language:  'zh-CN',
	    format: 'yyyy-mm-dd',
	    autoclose: true,
	    clearBtn:true
	});
});

function search(){
	  grid.pager.startRecord = 0;
	  grid.pager.nowPage = 1;
	  grid.parameters = new Object();
	  grid.parameters['startTime'] = $("#startTime").val();
	  grid.parameters['endTime'] = $("#endTime").val();
	  grid.refresh(true);
}
function editReleaseLimit(releaseDay,limitId,amount){
	limitEdit(releaseDay,limitId,amount,search);
}

function limitEdit(releaseDay,limitId,amount,callBack){
	$("#date").val(releaseDay);
	$("#amount").val(amount);
	$("#dateTd").html(releaseDay);
	$('#my-prompt').modal({
	    relatedTarget: this,
	    closeViaDimmer:false,
	    onConfirm: function(e) {
	    	$.ajax({
				url:"releaseLimit/editReleaseLimit",
				type:"post",
				dataType:"json",
				data:{"date":$("#date").val(),"id":limitId,"amount":$("#amount").val()},
				async:false,
				success:function(data){
					if(data.code==10000){
				         $('#my-prompt').modal('close');
				         layer.msg(data.msg, {
				        	 icon : 1
				         });
	                     if (callBack) {
	                    	 search();
	                     }
					}else{
						console.log(data.msg);
						 layer.msg(data.msg, {
		                        icon : 0
		                 });
					}
				},
				error:function(data){
				}  
			});
	    },
	    onCancel: function(e) {
	    }
	  });
}

